import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swipe_action_cell/core/cell.dart';
import 'package:get/get.dart';
import 'package:yp/config/config.dart';
import 'package:yp/helper.dart';

import '../../router.dart';
import 'logic.dart';
import 'state.dart';
import 'package:yp/pages/address_selector/address_model.dart';

class AddressListPage extends StatelessWidget {
  final AddressListLogic logic = Get.put(AddressListLogic());
  final AddressListState state = Get.find<AddressListLogic>().state;
  @override
  Widget build(BuildContext context) {
    // Get.back(result: "Refresh Now");
    return GetBuilder<AddressListLogic>(
      builder: (logic) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Config.mainColor,
            brightness: Brightness.light,
            iconTheme: IconThemeData(color: Colors.white),
            title: Text(
              state.isPersonalCenter ? '我的地址' : "选择收货地址",
              style: TextStyle(color: Colors.white, fontSize: 32.sp),
            ),
            centerTitle: true,
            elevation: 0,
          ),
          body: GetBuilder<AddressListLogic>(
            builder: (logic) {
              return Stack(
                children: [
                  Column(
                    children: [
                      Expanded(
                        child: ListView(
                          shrinkWrap: true,
                          primary: true,
                          children: [
                            Column(
                              children: [
                                //商品部分
                                Container(
                                  color: Colors.white,
                                  child: Column(
                                    children: [
                                      ListView.builder(
                                        itemCount: logic.state.goodsList.length,
                                        itemBuilder: (ctx, index) {
                                          bool isSelect = false;
                                          if (Get.arguments != null) {
                                            if (Get.arguments['adr_num_id'] !=
                                                null) {
                                              if (logic
                                                      .state
                                                      .goodsList[index]
                                                          ["adr_num_id"]
                                                      .toString() ==
                                                  Get.arguments['adr_num_id']
                                                      .toString()) {
                                                isSelect = true;
                                              }
                                            } else {
                                              if (logic
                                                      .state
                                                      .goodsList[index]
                                                          ["default_sign"]
                                                      .toString() ==
                                                  '1') {
                                                isSelect = true;
                                              }
                                            }
                                          }
                                          return SwipeActionCell(
                                            key: ObjectKey(
                                                logic.state.goodsList[index]),
                                            trailingActions: <SwipeAction>[
                                              SwipeAction(
                                                icon: Icon(
                                                  Icons.delete_forever_outlined,
                                                  color: Colors.white,
                                                ),
                                                onTap: (CompletionHandler
                                                    handler) {
                                                  showDialog(
                                                    context: context,
                                                    barrierDismissible: false,
                                                    builder:
                                                        (BuildContext context) {
                                                      return AlertDialog(
                                                        title: Text('提示'),
                                                        content: Text('是否删除地址'),
                                                        actions: <Widget>[
                                                          FlatButton(
                                                            onPressed: () =>
                                                                Navigator.of(
                                                                        context)
                                                                    .pop(),
                                                            child: Text('取消'),
                                                          ),
                                                          FlatButton(
                                                            onPressed: () {
                                                              logic.dedAss(state
                                                                          .goodsList[
                                                                      index][
                                                                  'adr_num_id']);
                                                              Navigator.of(
                                                                      context)
                                                                  .pop();
                                                            },
                                                            textColor:
                                                                Colors.red,
                                                            child: Text('确定'),
                                                          ),
                                                        ],
                                                      );
                                                    },
                                                  );
                                                  //删除当前项数据
                                                  // logic.state.cartData["multi_shop_items"][widget.shopIndex]["cart_item_list"][widget.goodsListIndex]["items"].entity.removeAt(widget.index);
                                                  // logic.state.cartData["multi_shop_items"][widget.shopIndex]["cart_item_list"][widget.goodsListIndex]["items"].delete(widget.index);
                                                },
                                                color: Colors.red,
                                              ),
                                            ],
                                            child: Container(
                                              child: Column(
                                                children: [
                                                  GestureDetector(
                                                    onTap: () {
                                                      if (!state
                                                          .isPersonalCenter) {
                                                        state.selectAdr = index;
                                                        // print(logic.state.goodsList[index]);
                                                        changeSelectAddress(logic.state.goodsList[index]);
                                                        Get.back(result: {
                                                          "address": logic.state
                                                              .goodsList[index],
                                                          "userNumId":
                                                              state.userNumid
                                                        });
                                                      }
                                                    },
                                                    child: Container(
                                                      padding:
                                                          EdgeInsets.all(30.w),
                                                      child: Row(
                                                        mainAxisAlignment:
                                                            MainAxisAlignment
                                                                .spaceBetween,
                                                        crossAxisAlignment:
                                                            CrossAxisAlignment
                                                                .center,
                                                        children: [
                                                          Offstage(
                                                            offstage: state
                                                                .isPersonalCenter,
                                                            child: isSelect
                                                                ? Container(
                                                                    padding: EdgeInsets.only(
                                                                        right: 10
                                                                            .w),
                                                                    child: Image
                                                                        .asset(
                                                                      "assets/images/selectIcon1.png",
                                                                      width:
                                                                          35.w,
                                                                    ),
                                                                  )
                                                                : Container(
                                                                    padding: EdgeInsets.only(
                                                                        right: 10
                                                                            .w),
                                                                    child: Image
                                                                        .asset(
                                                                      "assets/images/select1.png",
                                                                      width:
                                                                          35.w,
                                                                    ),
                                                                  ),
                                                          ),
                                                          Container(
                                                            width: 600.w,
                                                            child: Column(
                                                              mainAxisAlignment:
                                                                  MainAxisAlignment
                                                                      .start,
                                                              crossAxisAlignment:
                                                                  CrossAxisAlignment
                                                                      .start,
                                                              children: [
                                                                RichText(
                                                                  text:
                                                                      TextSpan(
                                                                    children: [
                                                                      TextSpan(
                                                                        text: logic
                                                                            .state
                                                                            .goodsList[index]["cont_empe"]
                                                                            .toString(),
                                                                        style:
                                                                            TextStyle(
                                                                          color:
                                                                              Colors.black,
                                                                          fontSize:
                                                                              28.w,
                                                                        ),
                                                                      ),
                                                                      WidgetSpan(
                                                                        child:
                                                                            SizedBox(
                                                                          width:
                                                                              10.w,
                                                                        ),
                                                                      ),
                                                                      TextSpan(
                                                                        text: logic
                                                                            .state
                                                                            .goodsList[index]["contact_no"]
                                                                            .toString(),
                                                                        style:
                                                                            TextStyle(
                                                                          color:
                                                                              Colors.black,
                                                                          fontSize:
                                                                              28.w,
                                                                        ),
                                                                      ),
                                                                      WidgetSpan(
                                                                        child:
                                                                            SizedBox(
                                                                          width:
                                                                              10.w,
                                                                        ),
                                                                      ),
                                                                      (logic.state.goodsList[index]["address_tag"] != null &&
                                                                              logic.state.goodsList[index]["address_tag"] != '')
                                                                          ? WidgetSpan(
                                                                              child: Container(
                                                                                padding: EdgeInsets.symmetric(horizontal: setWidth(13), vertical: setWidth(3)),
                                                                                decoration: BoxDecoration(
                                                                                  color: Colors.red,
                                                                                  borderRadius: BorderRadius.circular(setWidth(20)),
                                                                                ),
                                                                                child: Text(
                                                                                  logic.state.goodsList[index]["address_tag"] ?? '',
                                                                                  style: TextStyle(
                                                                                    color: Colors.white,
                                                                                    fontSize: 18.sp,
                                                                                    height: 1,
                                                                                  ),
                                                                                ),
                                                                              ),
                                                                            )
                                                                          : WidgetSpan(
                                                                              child: SizedBox(
                                                                                width: 0.w,
                                                                              ),
                                                                            ),
                                                                      logic.state.goodsList[index]["default_sign"].toString() ==
                                                                              '1'
                                                                          ? WidgetSpan(
                                                                              child: Container(
                                                                                margin: EdgeInsets.only(left: 10.w),
                                                                                padding: EdgeInsets.symmetric(horizontal: 5.w, vertical: 3.w),
                                                                                color: Config.mainColor,
                                                                                child: Text(
                                                                                  "默认",
                                                                                  style: TextStyle(
                                                                                    fontSize: 20.sp,
                                                                                    color: Colors.white,
                                                                                    height: 1,
                                                                                  ),
                                                                                ),
                                                                              ),
                                                                            )
                                                                          : WidgetSpan(
                                                                              child: SizedBox(
                                                                                width: 0.w,
                                                                              ),
                                                                            ),
                                                                    ],
                                                                  ),
                                                                ),
                                                                SizedBox(
                                                                  height: 10.w,
                                                                ),
                                                                Text(
                                                                  "${logic.state.goodsList[index]["prv_name"]} ${logic.state.goodsList[index]["city_name"]} ${logic.state.goodsList[index]["city_area_name"]} ${logic.state.goodsList[index]["town_name"] ?? ''} ${logic.state.goodsList[index]["adr"]}",
                                                                  style:
                                                                      TextStyle(
                                                                    color: Colors
                                                                        .black,
                                                                    fontSize:
                                                                        26.sp,
                                                                  ),
                                                                  maxLines: 2,
                                                                  textAlign:
                                                                      TextAlign
                                                                          .start,
                                                                  overflow:
                                                                      TextOverflow
                                                                          .ellipsis,
                                                                ),
                                                              ],
                                                            ),
                                                          ),
                                                          GestureDetector(
                                                            onTap: () {
                                                              print(logic.state
                                                                      .goodsList[
                                                                  index]);
                                                              Get.toNamed(
                                                                      RouteConfig
                                                                          .addressUpdate,
                                                                      arguments: logic
                                                                          .state
                                                                          .goodsList[index])
                                                                  .then((value) {
                                                                if (value !=
                                                                        '' &&
                                                                    value !=
                                                                        null) {
                                                                  //刷新列表页
                                                                  logic
                                                                      .onReady();
                                                                  logic
                                                                      .update();
                                                                }
                                                              });
                                                            },
                                                            child: Container(
                                                              padding: EdgeInsets
                                                                  .only(
                                                                      left:
                                                                          5.w),
                                                              child:
                                                                  Image.asset(
                                                                "assets/images/editIcon.png",
                                                                width: 35.w,
                                                              ),
                                                            ),
                                                          ),
                                                        ],
                                                      ),
                                                    ),
                                                  ),
                                                  Divider(
                                                    color: Color(0xffcccccc),
                                                  ),
                                                ],
                                              ),
                                            ),
                                          );
                                        },
                                        shrinkWrap: true,
                                        physics: NeverScrollableScrollPhysics(),
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            )
                          ],
                        ),
                      ),
                      Container(
                        color: Colors.white,
                        width: 750.w,
                        height: 100.w,
                        padding: EdgeInsets.symmetric(
                            horizontal: 45.w, vertical: 10.w),
                        child: GestureDetector(
                          onTap: () {
                            Get.toNamed(RouteConfig.addressCreate,
                                    arguments: {"userNum": state.userNumid})
                                .then((value) {
                              if (value != '' && value != null) {
                                //刷新列表页
                                logic.onReady();
                                logic.update();
                              }
                            });
                          },
                          child: Container(
                            height: 80.w,
                            width: 660.w,
                            decoration: BoxDecoration(
                              color: Config.mainColor,
                              borderRadius: BorderRadius.circular(20.w),
                            ),
                            child: Center(
                              child: Text(
                                "+新增地址",
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 28.w,
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              );
            },
          ),
        );
      },
    );
  }
}
